import { Element } from 'diagram-js/lib/model/Types';
import { ElMessage } from 'element-plus';
import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';
import modelerStore from '@wf/components/bpmnjs/store/modeler';
import editor from '@wf/components/bpmnjs/store/editor';

// 消息事件的增删改查方法
export function getGlobalMessageEventsValue() {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const messageEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Message'));
  return messageEvents;
}

export function setGlobalMessageEventsValue(messageEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const bpmnFactory = modeler.get('bpmnFactory');
  const newMessageRef = bpmnFactory.create('bpmn:Message');

  newMessageRef.name = messageEvent.name;
  newMessageRef.id = messageEvent.id;
  if (definitions.rootElements.find((e: Element) => e.id == newMessageRef.id)) {
    ElMessage.error('事件标识已存在');
    throw new Error('事件标识已存在');
  }

  definitions.rootElements.push(newMessageRef);
}

export function updateGlobalMessageEventsValue(messageEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const messageEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Message'));

  const id = messageEvent.id;
  const editMessageRef = messageEvents.find((e: Element) => e.id == id);
  if (editMessageRef) {
    editMessageRef.name = messageEvent.name;
  } else {
    ElMessage.error('消息事件标识未存在');
    throw new Error('消息事件标识未存在');
  }
}

export function deleteGlobalMessageEventsValue(messageEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const id = messageEvent.id;
  const messageRefIndex = definitions.rootElements.findIndex((e: Element) => is(e, 'bpmn:Message') && e.id == id);
  if (messageRefIndex > -1) {
    definitions.rootElements.splice(messageRefIndex, 1);
  } else {
    ElMessage.error('消息事件标识未存在');
    throw new Error('消息事件标识未存在');
  }
}

// 错误事件的增删改查方法
export function getGlobalErrorEventsValue() {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const errorEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Error'));
  return errorEvents;
}

export function setGlobalErrorEventsValue(errorEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const bpmnFactory = modeler.get('bpmnFactory');
  const newErrorRef = bpmnFactory.create('bpmn:Error');

  newErrorRef.name = errorEvent.name;
  newErrorRef.id = errorEvent.id;
  newErrorRef.errorCode = errorEvent.code;
  if (definitions.rootElements.find((e: Element) => e.id == newErrorRef.id)) {
    ElMessage.error('事件标识已存在');
    throw new Error('事件标识已存在');
  }

  definitions.rootElements.push(newErrorRef);
}

export function updateGlobalErrorEventsValue(errorEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const errorEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Error'));

  const id = errorEvent.id;
  const editErrorRef = errorEvents.find((e: Element) => e.id == id);
  if (editErrorRef) {
    editErrorRef.name = errorEvent.name;
    editErrorRef.errorCode = errorEvent.code;
  } else {
    ElMessage.error('错误事件标识未存在');
    throw new Error('错误事件标识未存在');
  }
}

export function deleteGlobalErrorEventsValue(errorEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const id = errorEvent.id;
  const errorRefIndex = definitions.rootElements.findIndex((e: Element) => is(e, 'bpmn:Error') && e.id == id);
  if (errorRefIndex > -1) {
    definitions.rootElements.splice(errorRefIndex, 1);
  } else {
    ElMessage.error('消息事件标识未存在');
    throw new Error('消息事件标识未存在');
  }
}

// 信号事件的增删改查方法
export function getGlobalSignalEventsValue() {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const signalEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Signal'));
  return signalEvents;
}

export function setGlobalSignalEventsValue(signalEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const signalEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Signal'));
  const bpmnFactory = modeler.get('bpmnFactory');
  const newSignalnRef = bpmnFactory.create('bpmn:Signal');
  const prefix = editor().getProcessEngine;

  newSignalnRef.name = signalEvent.name;
  newSignalnRef.id = signalEvent.id;
  newSignalnRef.$attrs[`${prefix}:scope`] = signalEvent.scope;

  if (signalEvents.find((e: Element) => e.id == newSignalnRef.id)) {
    ElMessage.error('信号事件标识已存在');
    throw new Error('信号事件标识已存在');
  }

  definitions.rootElements.push(newSignalnRef);
}

export function updateGlobalSignalEventsValue(signalEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const signalEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Signal'));

  const id = signalEvent.id;
  const editSignalRef = signalEvents.find((e: Element) => e.id == id);
  if (editSignalRef) {
    const prefix = editor().getProcessEngine;
    editSignalRef.name = signalEvent.name;
    editSignalRef.$attrs[`${prefix}:scope`] = signalEvent.scope;
  } else {
    ElMessage.error('信号事件标识未存在');
    throw new Error('信号事件标识未存在');
  }
}

export function deleteGlobalSignalEventsValue(signalEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const id = signalEvent.id;
  const signalRefIndex = definitions.rootElements.findIndex((e: Element) => is(e, 'bpmn:Signal') && e.id == id);
  if (signalRefIndex > -1) {
    definitions.rootElements.splice(signalRefIndex, 1);
  } else {
    ElMessage.error('信号事件标识未存在');
    throw new Error('信号事件标识未存在');
  }
}

// 升级事件的增删改查方法
export function getGlobalEscalationEventsValue() {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const escalationEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Escalation'));
  return escalationEvents;
}

export function setGlobalEscalationEventsValue(escalationEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const bpmnFactory = modeler.get('bpmnFactory');
  const newEscalationRef = bpmnFactory.create('bpmn:Escalation');

  newEscalationRef.name = escalationEvent.name;
  newEscalationRef.id = escalationEvent.id;
  newEscalationRef.escalationCode = escalationEvent.code;

  if (definitions.rootElements.find((e: Element) => e.id == newEscalationRef.id)) {
    ElMessage.error('事件标识已存在');
    throw new Error('事件标识已存在');
  }

  definitions.rootElements.push(newEscalationRef);
}

export function updateGlobalEscalationEventsValue(escalationEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();
  const escalationEvents = definitions.rootElements.filter((e: Element) => is(e, 'bpmn:Escalation'));

  const id = escalationEvent.id;
  const editEscalationRef = escalationEvents.find((e: Element) => e.id == id);
  if (editEscalationRef) {
    editEscalationRef.name = escalationEvent.name;
    editEscalationRef.escalationCode = escalationEvent.code;
  } else {
    ElMessage.error('升级事件标识未存在');
    throw new Error('升级事件标识未存在');
  }
}

export function deleteGlobalEscalationEventsValue(escalationEvent: any) {
  const store = modelerStore();
  const modeler = store.getModeler;
  const definitions = modeler.getDefinitions();

  const id = escalationEvent.id;
  const escalationRefIndex = definitions.rootElements.findIndex((e: Element) => is(e, 'bpmn:Escalation') && e.id == id);
  if (escalationRefIndex > -1) {
    definitions.rootElements.splice(escalationRefIndex, 1);
  } else {
    ElMessage.error('升级事件标识未存在');
    throw new Error('升级事件标识未存在');
  }
}
